using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;

namespace Manaddiction.UI
{
    public class PlayerInfoPanel : UIPanel
    {
        public TMP_InputField PlayerNameInput;
        public TextMeshProUGUI PlayerName;
        
        private static PlayerInfoPanel instance;

        protected override void Awake()
        {
            base.Awake();
            instance = this;
        }
        
        protected override void Start()
        {
            base.Start();
    
            // 不要调用 Show()，保持隐藏
            StartCoroutine(DelayedInitPlayerInfo());
        }

        private IEnumerator DelayedInitPlayerInfo()
        {
            yield return new WaitForSeconds(0.1f); // 短暂延迟
    
            if (GameDataManager.Instance != null && GameDataManager.Instance.userData != null)
            {
                UserData udata = GameDataManager.Instance.userData;
                PlayerName.text = udata.username;
                PlayerNameInput.text =  udata.username;
            }
        }

        public void OnSaveClick()
        {
            GameDataManager.Instance.userData.username =  PlayerNameInput.text;
            GameDataManager.Instance.SavePlayerData();
            TipsPanel.Get().ShowTips("Name Change Succeed");
        }
        
        public static PlayerInfoPanel Get()
        {
            return instance;
        }
    }
}